package com.ycxy.sh.transaction.controller.user;


import com.ycxy.sh.transaction.service.LoginService;
import com.ycxy.sh.transaction.service.UserInfoService;
import com.ycxy.sh.transaction.service.UserRoleService;
import com.ycxy.sh.transaction.util.*;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.dao.entity.Login;
import com.ycxy.sh.transaction.dao.entity.UserInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *  登录注册 控制器
 *
 * @author hlt
 * @since 2019-12-21
 */
@Api(tags = "2.用户管理模块")
@Controller
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserRoleService userRoleService;

    /**
     *图片验证码
     * */
    @RequestMapping(value = "/images", method = {RequestMethod.GET, RequestMethod.POST})
    public void images(HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        ValidateCode vCode = new ValidateCode(820, 200, 5, 80);
        vCode.write(response.getOutputStream());
    }
    /**注册时发送短信验证码
     * 1.判断是否为注册类型验证码
     * 2.判断手机号格式是否正确
     * 3.判断手机号是否已经注册过
     * 4.发送注册验证码并存入map集合
     * */
    @ResponseBody
    @PostMapping("/user/sendregcode")
    public JsonResult sendCode(HttpServletRequest request) throws IOException{
        JSONObject jsonObject = JsonReader.receivePost(request);
        final String mobilePhone = jsonObject.getString("mobilephone");
        int type = jsonObject.getInt("type");
        loginService.sendCode(type,mobilePhone,request);

        return JsonResult.ok();
    }

    /**注册
     * 1.前端传入用户名（username）、密码（password）、邮箱（email）、手机号（mobilephone）、验证码（verCode）
     * 2.查询账号是否已经注册
     * 3.查询用户名是否已存在
     * 4.判断验证码是否有效或正确
     * 5.注册
     * */
    @ResponseBody
    @PostMapping("/user/register")
    public JsonResult userRegister(@RequestBody UserInfo userInfo, HttpSession session) {
       loginService.loginRegister(userInfo,session);
       log.debug("该用户已经成功注册！");
       return JsonResult.ok();
    }

    /** 登录
     * 1.判断输入账号的类型
     * 2.登录
     * */
    @ResponseBody
    @PostMapping("/user/login")
    public JsonResult userLogin(@RequestBody Login login, HttpSession session){
        loginService.userStaredLogin(login,session);
        log.debug("用户登录成功");
        return JsonResult.ok();
    }

    /**重置密码时发送短信验证码
     * 1.判断是否为重置密码类型验证码
     * 2.判断手机号格式是否正确
     * 3.查询账号是否存在
     * 4.发送验证码
     * */
    @ResponseBody
    @PostMapping("/user/sendresetpwd")
    public JsonResult sendResetPassword(HttpServletRequest request) throws IOException {
        JSONObject json = JsonReader.receivePost(request);
        final String mobilephone = json.getString("mobilephone");
        Integer type = json.getInt("type");
        loginService.sendResetPassword(type,mobilephone,request);
        log.debug("短信验证码发送成功");
        return JsonResult.ok();
    }

    /**重置密码
     * 1.判断手机号格式是否正确
     * 2.查询手机号是否存在
     * 3.判断验证码是否有效或正确
     * 4.重置密码
     * */
    @ResponseBody
    @PostMapping("/user/resetpwd")
    public JsonResult resetPassword(@RequestBody Login login) {
        log.debug("重置密码成功");
        loginService.resetPassword(login);
        return JsonResult.ok();
    }

    /**退出登陆*/
    @GetMapping("/user/logout")
    public String logout(HttpServletRequest request,HttpSession session){
        String userid = (String)session.getAttribute("userid");
        String username = (String)session.getAttribute("username");
        if(StringUtils.isEmpty(userid) && StringUtils.isEmpty(username)){
            return "redirect:/";
        }
        request.getSession().removeAttribute("userid");
        request.getSession().removeAttribute("username");
        return "redirect:/";
    }


}